package wty;

import management.Management;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.*;

public class Login {

    //所有内容都加到panel中
    public static JPanel panel;


    //管理员信息
    //默认为root root
    private String administrator_ID = "root";

    public void setAdministrator_ID(String administrator_ID) {
        this.administrator_ID = administrator_ID;
    }

    private static String administrator_Password = "root";

    public static void setAdministrator_Password(String administrator_Password) {
        Login.administrator_Password = administrator_Password;
    }

//    private String administrator_ID = "0";
//    private String administrator_Password = "0";

    JTextField useNameField;
    JTextField userField;
    JPasswordField passwordField;


    public Login(){
        init();
    }

    private void init(){
//        //数据库操作
//        mysql();

        // 创建主面板，使用BorderLayout
        panel = new JPanel();
        panel.setLayout(new BorderLayout(20, 20)); // 设置水平和垂直间距为20

        // 设置面板的背景色
        panel.setBackground(Color.CYAN); // 浅蓝色背景

        // 创建顶部标签
        JLabel label = new JLabel("Welcome to the Login!", JLabel.CENTER);
        label.setFont(new Font("Arial", Font.BOLD, 36));
//        label.setPreferredSize(new Dimension(0, 60)); // 设置标签的高度
        panel.add(label, BorderLayout.NORTH); // 将标签添加到上面

        // 创建中间输入面板
        JPanel inputPanel = new JPanel();
        inputPanel.setLayout(new GridLayout(3, 2, 10, 10));  // 改为3行2列的网格布局，间隔10像素
        inputPanel.setBackground(Color.lightGray);


        JLabel useNameLabel = new JLabel("Use Name(Can be null):");
        useNameLabel.setFont(new Font("Arial", Font.PLAIN, 28));
        useNameField = new JTextField();
        useNameField.setFont(new Font("SimSun", Font.PLAIN, 28));


        JLabel userLabel = new JLabel("User ID:");
        userLabel.setFont(new Font("Arial", Font.PLAIN, 28));
        userField = new JTextField();
        userField.setFont(new Font("Arial", Font.PLAIN, 28));

        JLabel passwordLabel = new JLabel("Password:");
        passwordLabel.setFont(new Font("Arial", Font.PLAIN, 28));
        passwordField = new JPasswordField();
        passwordField.setFont(new Font("Arial", Font.PLAIN, 28));

        inputPanel.add(useNameLabel);
        inputPanel.add(useNameField);
        inputPanel.add(userLabel);
        inputPanel.add(userField);
        inputPanel.add(passwordLabel);
        inputPanel.add(passwordField);

        // 将输入面板添加到中部
        panel.add(inputPanel, BorderLayout.CENTER);

        // 创建底部的按钮面板
        JPanel buttonPanel = new JPanel();

        //登录俺妞
        JButton loginButton = new JButton("Login");
        loginButton.setFont(new Font("Arial", Font.BOLD, 16));
        loginButton.setBackground(new Color(0, 102, 204));
        loginButton.setForeground(Color.WHITE); // 按钮文字颜色

        // 登录按钮事件处理
        loginButton.addActionListener(
                new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                String ID = userField.getText();
                String Passward = String.valueOf(passwordField.getPassword());
//                System.out.println(userID);
//                System.out.println(userID);
                if (Login_SQL.mysql(ID,Passward)){

                    //登陆成功

                    Login.panel.setVisible(false);

                    Management.panel.setVisible(true);
//                    Register.panel.setVisible(true);

                    //清空输入的内容
                    userField.setText("");
                    passwordField.setText("");
                    useNameField.setText("");

                    //传入ID,以便后续操作
                    Management.setID(ID);
                }
                else if(ID.equals(administrator_ID) && Passward.equals(administrator_Password)){
//                    System.out.println("管理员登陆成功");

                    Login.panel.setVisible(false);
                    Administrator.panel.setVisible(true);

                    userField.setText("");
                    passwordField.setText("");
                    useNameField.setText("");

                }
                else {
                    label.setText("Error!Invalid Username or Password!");
                    label.setForeground(Color.RED);
                    userField.setText("");
                    passwordField.setText("");
                    useNameField.setText("");
                }

            }
        });

        // 将登录按钮添加到底部面板
        buttonPanel.add(loginButton);



        //进入注册按钮
        JButton registerButton = new JButton("Register");
        registerButton.setFont(new Font("Arial", Font.BOLD, 16));
        registerButton.setBackground(new Color(0, 102, 204)); // 按钮背景颜色
        registerButton.setForeground(Color.WHITE); // 按钮文字颜色

        registerButton.addActionListener(
                new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {

                        Login.panel.setVisible(false);
                        Register.panel.setVisible(true);
//                        System.out.println("进入注册");

                    }
                }
        );

        buttonPanel.add(registerButton);

        // 将底部面板添加到主面板的下面
        panel.add(buttonPanel, BorderLayout.SOUTH);

        Login.panel.setVisible(false);
    }

}
